import 'server-only' //代表仅服务端可用
import Joi from 'joi';
import resFn from '@/app/api/util/res'
import { NextRequest } from 'next/server';
import { logError, logSpecial } from '@/app/api/util/io/log';
import { getBody, getFormData, judge, permissionJudge } from '@/app/api/util/res/handle';
import { SliceUploadFromReq } from '@/types/api/file/sliceUpload';
import { writeFile } from '@/app/api/util/io';
import { ResCode, RejectData } from '@/app/api/util/res/code';
import { getTempDir, getTempFilePath } from '../../getPath';
export const dynamic = 'force-dynamic';

/**分片上传 - 上传切片 接口*/
export async function POST(request: NextRequest) {
    try {
        const user_id = "JZ8740"
        const [files, otherData] = await getFormData<SliceUploadFromReq>(request)
        logSpecial('上传切片接口', files, otherData)
        if (files[0] == undefined) throw new RejectData(ResCode.BAD_REQUEST, "文件为空！")
        await writeFile(getTempFilePath(user_id, otherData.md5, otherData.index), files[0]) //存在 /temp/userid_md5/  目录下
        return resFn.success<any>('操作成功');
    } catch (error: any) {
        logError('分片上传 - 上传切片 接口', error)
        return resFn.errorWithReject(error);
    }
}